<script setup>
import { artDelService, artGetListService } from '@/api/article'
import { formatTime } from '@/utils/format'
import { Delete, Edit } from '@element-plus/icons-vue'
import { ref } from 'vue'
import ArticleEdit from './components/ArticleEdit.vue'
import ChannelSelect from './components/ChannelSelect.vue'

const articleList = ref([])
const total = ref(0)
const loading = ref(false)
// 定义请求参数对象
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: '',
})
function handleSizeChange(newPageSize) {
  params.value.pagenum = 1
  params.value.pagesize = newPageSize
  getArticleList()
}
function handleCurrentChange(newPageNum) {
  params.value.pagenum = newPageNum
  getArticleList()
}
async function getArticleList() {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getArticleList()
const articleEditRef = ref()
// 添加逻辑
function onAddArticle() {
  articleEditRef.value.open({})
}
// 编辑逻辑
function onEditArticle(row) {
  articleEditRef.value.open(row)
}
// 删除逻辑
async function onDeleteArticle(row) {
  // eslint-disable-next-line no-undef
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  })
  await artDelService(row.id)
  // eslint-disable-next-line no-undef
  ElMessage({ type: 'success', message: '删除成功' })
  getArticleList()
}

// 搜索逻辑
function onSearch() {
  params.value.pagenum = 1
  getArticleList()
}
function onReset() {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}
// /添加或者编辑
function onSuccess(type) {
  if (type === 'add') {
    // 如果是添加，最好渲染最后一列
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    // \更新成最大页码条数
    params.value.pagenum = lastPage
  }
  getArticleList()
}
</script>

<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">
        添加文章
      </el-button>
    </template>
    <!-- 表单区域 -->
    <el-form :inline="true">
      <el-form-item label="文章分类">
        <!-- label展示给用户看的，value提交后台的 -->

        <!-- vue3中v-model是:modelValue和@update:modelValue的简写 -->
        <ChannelSelect v-model="params.cate_id" />
      </el-form-item>
      <el-form-item label="发布状态">
        <el-select v-model="params.state" style="width: 120px;">
          <!-- 这里后台标记发布，就是通过中文标记的，已发布/草稿 -->
          <el-option label="已发布" value="已发布" />
          <el-option label="草稿" value="草稿" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">
          搜索
        </el-button>
        <el-button @click="onReset">
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="articleList">
      <el-table-column label="文章标题" prop="title">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">
            {{ row.title }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name" />
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button circle plain type="primary" :icon="Edit" @click="onEditArticle(row)" />
          <el-button circle plain type="danger" :icon="Delete" @click="onDeleteArticle(row)" />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style=" display: flex; justify-content: flex-end;margin-top: 20px;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 抽屉 -->
    <ArticleEdit ref="articleEditRef" @success="onSuccess" />
  </page-container>
</template>

<style lang="scss" scoped></style>
